﻿using System;
using System.IO;
using BluePortal.Medias.Core;

namespace BluePortal.Encoding.Core.Providers

{
    /// <summary>
    /// ConnectionString format : UploadShareDirectory=c:\temp;VideosUrl=http://localhost:49352/httpvideo/
    /// </summary>
    public class ShareUploadService : BaseUploadService, IVideoUploadService
    {
        BCVideo bc = new BCVideo();
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="connectionParameters">Parameters name : UploadShareDirectory</param>
        /// <param name="vid"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        public void Upload(Video video, string directory)
        {
            string todir = ConnectionParameters["UploadShareDirectory"];
            todir = Path.Combine(todir, video.ID.ToString());
            if (!Directory.Exists(todir))
                Directory.CreateDirectory(todir);

            foreach (string file in GetFiles(directory))
            {
                string destFileName = Path.GetFileName(file);
                File.Copy(file, Path.Combine(todir, destFileName), true);
            }

            video.ProviderOptions = ConnectionParameters["VideosUrl"];
        }

        public override string GetUrlFromVID(Guid vid)
        {
            base.GetUrlFromVID(vid);
            Video v = bc.FindVideoByID(vid);
            return Concat(v.ProviderOptions, vid.ToString() + "/" + vid.ToString() + ".wmv");
        }

        public override string GetUrlFromVID(Guid vid, int formatId)
        {
            if (formatId == 0)
                return GetUrlFromVID(vid);
            else
            {
                base.GetUrlFromVID(vid, formatId);
                Video v = bc.FindVideoByID(vid);
                return Concat(v.ProviderOptions, vid.ToString() + "/" + vid.ToString() + "_" + formatId.ToString() + ".wmv");
            }
        }

        public void Delete(Guid vid)
        {
            throw new NotImplementedException();
        }

        private string Concat(string url, string suburl)
        {
            if (!url.EndsWith("/"))
                url += "/";
            url += suburl;
            return url;
        }

        
    }
}
